<template>
  <div class="app-container">
    <el-container style="height: 100vh; border: 1px solid #eee">
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-menu :default-openeds="['1']" router>
          <!-- 首页导航 -->
          <el-menu-item index="/home">
            <template slot="title"><i class="el-icon-s-home"></i>首页</template>
          </el-menu-item>
          
          <!-- 系统 -->
          <el-submenu index="1">
            <template slot="title"><i class="el-icon-message"></i>系统管理</template>
            <el-menu-item-group>
              <el-menu-item index="/users">用户列表</el-menu-item>
              <el-menu-item index="1-3">日志管理</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <el-header style="text-align: right; font-size: 12px">

          
          <!-- 用户下拉菜单 -->
          <el-dropdown>
            <span class="el-dropdown-link">
              {{ currentUser.username || '用户' }}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="goToProfile">个人空间</el-dropdown-item>
              <el-dropdown-item @click.native="goToSettings">个人设置</el-dropdown-item>
              <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        
        <el-main>
          <!-- 嵌套路由的视图容器 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { removeToken, getUserInfo, removeUserInfo, getToken } from '@/utils/token'

export default {
  name: 'home',
  data() {
    const item = {
      date: '2016-05-02',
      name: '王小虎',
      address: '上海市普陀区金沙江路 1518 弄'
    };
    return {
      tableData: Array(20).fill(item),
      currentUser: {}
    }
  },
  methods: {
    // 确保布局始终铺满全屏
    adjustLayout() {
      const windowHeight = window.innerHeight;
      document.documentElement.style.height = `${windowHeight}px`;
    },
    
    // 获取当前登录用户信息
    getCurrentUser() {
      const userInfo = getUserInfo();
      this.currentUser = userInfo || { username: '游客' };
    },
    
    // 跳转到个人空间
    goToProfile() {
      // 这里可以根据实际需求跳转到个人空间页面
      this.$message.info('跳转到个人空间');
    },
    
    // 跳转到个人设置
    goToSettings() {
      // 这里可以根据实际需求跳转到个人设置页面
      this.$message.info('跳转到个人设置');
    },
    
    // 退出登录方法
    logout() {
      this.$confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 移除token和用户信息
        removeToken();
        removeUserInfo();
        // 跳转到登录页面
        this.$router.push('/login');
        // 显示成功消息
        this.$message({
          type: 'success',
          message: '退出登录成功!'
        });
      }).catch(() => {
        // 用户取消退出
        this.$message({
          type: 'info',
          message: '已取消退出'
        });
      });
    }
  },
  mounted() {
    // 初始化布局
    this.adjustLayout();
    // 监听窗口大小变化
    window.addEventListener('resize', this.adjustLayout);
    // 获取当前登录用户信息
    this.getCurrentUser();
  },
  beforeDestroy() {
    // 移除事件监听，防止内存泄漏
    window.removeEventListener('resize', this.adjustLayout);
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
.el-main {
  padding: 20px;
  background-color: #f9f9f9;
}
</style>